﻿''=================================
'' file:ImageControl.vb
'' desc:图像控件
'' date:
'' author:
'' version: 1.0
''
'' History
''  version:1.1
''  author: lidalong
''  date:   2009-11-11
''  desc:   设置当文本为预警时，文本颜色为橙色。参见需求编号：3.4.3.1 R1.002 。
''
''  version:1.2
''  author: lidalong
''  date:   2010-04-22
''  desc:   修改控件字体技术算法
''===============================

''' <summary>
''' 图像控件
''' </summary>
''' <remarks>
''' 参见数据库表中 TbccImageControls
''' </remarks>
Public Class ImgControl

    Private Rezise_fin As Boolean = False '用户改变图元大小的操作是否完成
    Private _type As Integer

    Public Sub New(Optional ByVal type As Integer = 1)
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.UserPaint, True)
        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        InitializeComponent()
        _type = type
        '’   Init()
    End Sub


    Public Property ImgControlType() As Integer
        Get
            Return _type
        End Get
        Set(ByVal value As Integer)
            _type = value
        End Set
    End Property


    Public Event DeleteEvent As EventHandler

    '用户删除图元
    Private Sub ImgControl_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.Delete Then
            RaiseEvent DeleteEvent(Me, New EventArgs)
        End If
    End Sub


    Private Sub ImgControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        AddHandler Me.DoubleClick, AddressOf AllControl_DoubleClick
        For Each c As Control In Me.Controls
            AddHandler c.DoubleClick, AddressOf AllControl_DoubleClick
        Next

        'If _ici IsNot Nothing Then
        '    SetMsg()
        'End If

    End Sub

    Public Event ShowDetailsEvent As EventHandler

    Private Sub AllControl_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
        RaiseEvent ShowDetailsEvent(Me, New EventArgs)
    End Sub

    '包括的imgControlItem对象(与数据库进行映射)
    Private _ici As ImgControlItem
    Public Property ici() As ImgControlItem
        Get
            Return _ici
        End Get
        Set(ByVal value As ImgControlItem)
            _ici = value
            SetMsg()
        End Set
    End Property

    '读取或设置当前的温湿度值
    Private _value As String
    Private _alarm As String
    ''' <summary>
    ''' 设置或获取Value
    ''' </summary>
    ''' <remarks>设置值时，同步更新颜色</remarks>
    Public Property alarm() As String
        Get
            Return Me.lblvalue.Text
        End Get
        Set(ByVal alarm As String)


            Select Case alarm
                Case 3, 4
                    Me.lblvalue.ForeColor = Color.Red
                Case 1, 2
                    Me.lblvalue.ForeColor = Color.Orange
                Case Else
                    Me.lblvalue.ForeColor = Color.Black
            End Select

            Me.lblvalue.Text = alarm
            '   RefreshControlSize()
            ' ResizeChild()
        End Set
    End Property

    ''' <summary>
    ''' 设置或获取Value
    ''' </summary>
    ''' <remarks>设置值时，同步更新颜色</remarks>
    Public Property value() As String
        Get
            Return Me.lblvalue.Text
        End Get
        Set(ByVal value As String)
            'If value = "正常" OrElse value = "报警" Then
            If IsNumeric(value) = False Then '是否为数字

                ''==============================
                '' author:  李大龙
                '' date:    2009-11-11
                '' cause:   设置“预警”时颜色为橙色
                '' see:     需求NO：3.4.3.1 R1 002
                Select Case value
                    Case "报警"
                        Me.lblvalue.ForeColor = Color.Red
                    Case "预警"
                        Me.lblvalue.ForeColor = Color.Orange
                    Case "开启"
                        Me.lblvalue.ForeColor = Color.Orange
                    Case "运行"
                        Me.lblvalue.ForeColor = Color.Orange
                    Case "正常"
                        Me.lblvalue.ForeColor = Color.Black
                    Case "##.##"
                        Me.lblvalue.ForeColor = Color.Black
                    Case "关闭"
                        Me.lblvalue.ForeColor = Color.Black
                    Case "停止"
                        Me.lblvalue.ForeColor = Color.Black
                    Case Else
                        Me.lblvalue.ForeColor = Color.Black

                End Select

                Me.lblvalue.Text = value
            Else
                Me.lblvalue.Text = String.Format(CInt(value * 100) / 100.0, "0.00")
            End If

            '   RefreshControlSize()
            ' ResizeChild()
        End Set
    End Property

    '当前是否出于设计状态 
    Private _IsDesign As Boolean

    Public Property IsDesign() As Boolean
        Get
            Return _IsDesign
        End Get
        Set(ByVal value As Boolean)
            _IsDesign = value
        End Set
    End Property


    Private Sub SetMsg()
        If (_type = 5) Then
            Me.TitleMsg.Text = _ici.TitleMsg.Replace("预警", "状态")
        ElseIf (_type = 6) Then
            Me.TitleMsg.Text = _ici.TitleMsg.Replace("状态", "预警")
        Else
            Me.TitleMsg.Text = _ici.TitleMsg
        End If

        'If (_type = 6) Then
        '    Me.TitleMsg.Text = _ici.TitleMsg.Replace("状态", "预警")
        'Else
        '    Me.TitleMsg.Text = _ici.TitleMsg
        'End If

        Me.UnitMsg.Text = _ici.UnitMsg
        Me.ToolTip1.SetToolTip(Me, _ici.TipMsg)
        If _ici.DataType = 1 OrElse _ici.DataType = 2 Then '温湿度类型
            Me.lblvalue.Text = "##.## "

        Else : _ici.DataType = 3   '报警类型
            If ici.PortNo = 15 Then
                Me.lblvalue.Text = "##.## "
            Else
                Me.lblvalue.Text = "无数据"

            End If


        End If


        For Each ctl As Control In Me.Controls
            Me.ToolTip1.SetToolTip(ctl, _ici.TipMsg)
        Next
        If Not _IsDesign Then
            ResizeChild()
        End If

        Me.UnitMsg.Left = Me.lblvalue.Right + Me.UnitMsg.Margin.Left + Me.lblvalue.Margin.Right
    End Sub

    Public Sub InitFont()

        Dim iCount As Integer = FontCalculateHelper.CharactersCount(Me.TitleMsg.Text + Me.lblvalue.Text + Me.UnitMsg.Text)
        Me.MinimumSize = New Size(iCount * 4 + 25 + 2, 15)

        Dim TSize As Size = New System.Drawing.Size(0, 0)
        LT.Font = New Font("宋体", 9)
        LT.Text = Me.TitleMsg.Text
        LT.Invalidate(True)
        TSize = LT.Size

        LT.Text = Me.UnitMsg.Text
        LT.Invalidate(True)
        TSize.Width += LT.Size.Width

        LT.Font = New Font("宋体", 9, FontStyle.Bold)
        LT.Text = lblvalue.Text
        LT.Invalidate(True)
        TSize.Width += LT.Size.Width


        TSize.Height += 1
        TSize.Width += 1 + Me.UnitMsg.Margin.Left + Me.UnitMsg.Margin.Right + Me.lblvalue.Margin.Left + Me.lblvalue.Margin.Right + Me.TitleMsg.Margin.Right + 5

        Me.Size = TSize
    End Sub

    Private Sub Init()
        Me.TitleMsg.Text = ""
        Me.UnitMsg.Text = ""
        Me.ToolTip1.SetToolTip(Me, "")
        For Each ctl As Control In Me.Controls
            Me.ToolTip1.SetToolTip(ctl, "")
        Next
        _IsDesign = True
    End Sub

    Private Sub ImgControl_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        Rezise_fin = True
    End Sub

    Private Sub ImgControl_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        Rezise_fin = False
    End Sub

    Private Sub ImgControl_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
        ResizeChild()
        Rezise_fin = True
    End Sub


    Private Sub ResizeChild()

        Dim SP As Size = Me.ClientSize

        SP.Width = SP.Width - (Me.TitleMsg.Margin.Right + Me.lblvalue.Margin.Right + Me.lblvalue.Margin.Left + Me.UnitMsg.Margin.Left + Me.TitleMsg.Margin.Right + 5)

        Dim FontWidth As Integer
        If (Me.UnitMsg.Text.Length > 0) Then
            FontWidth = SP.Width / FontCalculateHelper.CharactersCount(Me.TitleMsg.Text + Me.lblvalue.Text + Me.UnitMsg.Text + "%")
        Else
            FontWidth = SP.Width / FontCalculateHelper.CharactersCount(Me.TitleMsg.Text + Me.lblvalue.Text)
        End If
        Dim FontHeight As Integer = SP.Height
        FontHeight = Math.Min(FontWidth * 2, FontHeight)
        Dim ft As Font = FontCalculateHelper.AdjustFont(FontHeight)
        Dim TSize As Size = New System.Drawing.Size(0, 0)
        LT.AutoSize = True

        ' Do

        LT.Font = ft
        LT.Text = TitleMsg.Text
        LT.Invalidate()
        TSize = LT.Size

        LT.Text = UnitMsg.Text
        LT.Invalidate()
        TSize.Width += LT.Size.Width

        LT.Font = New Font(ft.FontFamily, ft.Size, FontStyle.Bold)
        LT.Invalidate()
        TSize.Width += LT.Size.Width

        FontHeight -= 1
        ft = FontCalculateHelper.AdjustFont(FontHeight)
        ' Loop While (TSize.Width > SP.Width Or TSize.Height > SP.Height)

        ''===========================
        '' 顺序不能颠倒
        TitleMsg.Font = ft
        lblvalue.Font = New Font(ft.FontFamily, ft.Size, FontStyle.Bold)
        UnitMsg.Font = ft
    End Sub


    Private Sub TitleMsg_SizeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TitleMsg.SizeChanged
        Me.lblvalue.Left = TitleMsg.Right + Me.TitleMsg.Margin.Right + Me.lblvalue.Margin.Left
    End Sub

    Private Sub lblvalue_SizeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblvalue.SizeChanged
        Me.UnitMsg.Left = Me.lblvalue.Right + Me.UnitMsg.Margin.Left + Me.lblvalue.Margin.Right
    End Sub
End Class

